<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>listenable.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_events_listenable.js.html">listenable.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview An interface for a listenable JavaScript object.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.events.Listenable&#39;);
<a name="line20"></a>goog.provide(&#39;goog.events.ListenableKey&#39;);
<a name="line21"></a>
<a name="line22"></a>/** @suppress {extraRequire} */
<a name="line23"></a>goog.require(&#39;goog.events.EventId&#39;);
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>/**
<a name="line28"></a> * A listenable interface. A listenable is an object with the ability
<a name="line29"></a> * to dispatch/broadcast events to &quot;event listeners&quot; registered via
<a name="line30"></a> * listen/listenOnce.
<a name="line31"></a> *
<a name="line32"></a> * The interface allows for an event propagation mechanism similar
<a name="line33"></a> * to one offered by native browser event targets, such as
<a name="line34"></a> * capture/bubble mechanism, stopping propagation, and preventing
<a name="line35"></a> * default actions. Capture/bubble mechanism depends on the ancestor
<a name="line36"></a> * tree constructed via {@code #getParentEventTarget}; this tree
<a name="line37"></a> * must be directed acyclic graph. The meaning of default action(s)
<a name="line38"></a> * in preventDefault is specific to a particular use case.
<a name="line39"></a> *
<a name="line40"></a> * Implementations that do not support capture/bubble or can not have
<a name="line41"></a> * a parent listenable can simply not implement any ability to set the
<a name="line42"></a> * parent listenable (and have {@code #getParentEventTarget} return
<a name="line43"></a> * null).
<a name="line44"></a> *
<a name="line45"></a> * Implementation of this class can be used with or independently from
<a name="line46"></a> * goog.events.
<a name="line47"></a> *
<a name="line48"></a> * Implementation must call {@code #addImplementation(implClass)}.
<a name="line49"></a> *
<a name="line50"></a> * @interface
<a name="line51"></a> * @see goog.events
<a name="line52"></a> * @see http://www.w3.org/TR/DOM-Level-2-Events/events.html
<a name="line53"></a> */
<a name="line54"></a>goog.events.Listenable = function() {};
<a name="line55"></a>
<a name="line56"></a>
<a name="line57"></a>/**
<a name="line58"></a> * An expando property to indicate that an object implements
<a name="line59"></a> * goog.events.Listenable.
<a name="line60"></a> *
<a name="line61"></a> * See addImplementation/isImplementedBy.
<a name="line62"></a> *
<a name="line63"></a> * @type {string}
<a name="line64"></a> * @const
<a name="line65"></a> */
<a name="line66"></a>goog.events.Listenable.IMPLEMENTED_BY_PROP =
<a name="line67"></a>    &#39;closure_listenable_&#39; + ((Math.random() * 1e6) | 0);
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>/**
<a name="line71"></a> * Marks a given class (constructor) as an implementation of
<a name="line72"></a> * Listenable, do that we can query that fact at runtime. The class
<a name="line73"></a> * must have already implemented the interface.
<a name="line74"></a> * @param {!Function} cls The class constructor. The corresponding
<a name="line75"></a> *     class must have already implemented the interface.
<a name="line76"></a> */
<a name="line77"></a>goog.events.Listenable.addImplementation = function(cls) {
<a name="line78"></a>  cls.prototype[goog.events.Listenable.IMPLEMENTED_BY_PROP] = true;
<a name="line79"></a>};
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * @param {Object} obj The object to check.
<a name="line84"></a> * @return {boolean} Whether a given instance implements
<a name="line85"></a> *     Listenable. The class/superclass of the instance must call
<a name="line86"></a> *     addImplementation.
<a name="line87"></a> */
<a name="line88"></a>goog.events.Listenable.isImplementedBy = function(obj) {
<a name="line89"></a>  try {
<a name="line90"></a>    return !!(obj &amp;&amp; obj[goog.events.Listenable.IMPLEMENTED_BY_PROP]);
<a name="line91"></a>  } catch (e) {
<a name="line92"></a>    return false;
<a name="line93"></a>  }
<a name="line94"></a>};
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Adds an event listener. A listener can only be added once to an
<a name="line99"></a> * object and if it is added again the key for the listener is
<a name="line100"></a> * returned. Note that if the existing listener is a one-off listener
<a name="line101"></a> * (registered via listenOnce), it will no longer be a one-off
<a name="line102"></a> * listener after a call to listen().
<a name="line103"></a> *
<a name="line104"></a> * @param {string|!goog.events.EventId.&lt;EVENTOBJ&gt;} type The event type id.
<a name="line105"></a> * @param {function(this:SCOPE, EVENTOBJ):(boolean|undefined)} listener Callback
<a name="line106"></a> *     method.
<a name="line107"></a> * @param {boolean=} opt_useCapture Whether to fire in capture phase
<a name="line108"></a> *     (defaults to false).
<a name="line109"></a> * @param {SCOPE=} opt_listenerScope Object in whose scope to call the
<a name="line110"></a> *     listener.
<a name="line111"></a> * @return {goog.events.ListenableKey} Unique key for the listener.
<a name="line112"></a> * @template SCOPE,EVENTOBJ
<a name="line113"></a> */
<a name="line114"></a>goog.events.Listenable.prototype.listen;
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Adds an event listener that is removed automatically after the
<a name="line119"></a> * listener fired once.
<a name="line120"></a> *
<a name="line121"></a> * If an existing listener already exists, listenOnce will do
<a name="line122"></a> * nothing. In particular, if the listener was previously registered
<a name="line123"></a> * via listen(), listenOnce() will not turn the listener into a
<a name="line124"></a> * one-off listener. Similarly, if there is already an existing
<a name="line125"></a> * one-off listener, listenOnce does not modify the listeners (it is
<a name="line126"></a> * still a once listener).
<a name="line127"></a> *
<a name="line128"></a> * @param {string|!goog.events.EventId.&lt;EVENTOBJ&gt;} type The event type id.
<a name="line129"></a> * @param {function(this:SCOPE, EVENTOBJ):(boolean|undefined)} listener Callback
<a name="line130"></a> *     method.
<a name="line131"></a> * @param {boolean=} opt_useCapture Whether to fire in capture phase
<a name="line132"></a> *     (defaults to false).
<a name="line133"></a> * @param {SCOPE=} opt_listenerScope Object in whose scope to call the
<a name="line134"></a> *     listener.
<a name="line135"></a> * @return {goog.events.ListenableKey} Unique key for the listener.
<a name="line136"></a> * @template SCOPE,EVENTOBJ
<a name="line137"></a> */
<a name="line138"></a>goog.events.Listenable.prototype.listenOnce;
<a name="line139"></a>
<a name="line140"></a>
<a name="line141"></a>/**
<a name="line142"></a> * Removes an event listener which was added with listen() or listenOnce().
<a name="line143"></a> *
<a name="line144"></a> * @param {string|!goog.events.EventId.&lt;EVENTOBJ&gt;} type The event type id.
<a name="line145"></a> * @param {function(this:SCOPE, EVENTOBJ):(boolean|undefined)} listener Callback
<a name="line146"></a> *     method.
<a name="line147"></a> * @param {boolean=} opt_useCapture Whether to fire in capture phase
<a name="line148"></a> *     (defaults to false).
<a name="line149"></a> * @param {SCOPE=} opt_listenerScope Object in whose scope to call
<a name="line150"></a> *     the listener.
<a name="line151"></a> * @return {boolean} Whether any listener was removed.
<a name="line152"></a> * @template SCOPE,EVENTOBJ
<a name="line153"></a> */
<a name="line154"></a>goog.events.Listenable.prototype.unlisten;
<a name="line155"></a>
<a name="line156"></a>
<a name="line157"></a>/**
<a name="line158"></a> * Removes an event listener which was added with listen() by the key
<a name="line159"></a> * returned by listen().
<a name="line160"></a> *
<a name="line161"></a> * @param {goog.events.ListenableKey} key The key returned by
<a name="line162"></a> *     listen() or listenOnce().
<a name="line163"></a> * @return {boolean} Whether any listener was removed.
<a name="line164"></a> */
<a name="line165"></a>goog.events.Listenable.prototype.unlistenByKey;
<a name="line166"></a>
<a name="line167"></a>
<a name="line168"></a>/**
<a name="line169"></a> * Dispatches an event (or event like object) and calls all listeners
<a name="line170"></a> * listening for events of this type. The type of the event is decided by the
<a name="line171"></a> * type property on the event object.
<a name="line172"></a> *
<a name="line173"></a> * If any of the listeners returns false OR calls preventDefault then this
<a name="line174"></a> * function will return false.  If one of the capture listeners calls
<a name="line175"></a> * stopPropagation, then the bubble listeners won&#39;t fire.
<a name="line176"></a> *
<a name="line177"></a> * @param {goog.events.EventLike} e Event object.
<a name="line178"></a> * @return {boolean} If anyone called preventDefault on the event object (or
<a name="line179"></a> *     if any of the listeners returns false) this will also return false.
<a name="line180"></a> */
<a name="line181"></a>goog.events.Listenable.prototype.dispatchEvent;
<a name="line182"></a>
<a name="line183"></a>
<a name="line184"></a>/**
<a name="line185"></a> * Removes all listeners from this listenable. If type is specified,
<a name="line186"></a> * it will only remove listeners of the particular type. otherwise all
<a name="line187"></a> * registered listeners will be removed.
<a name="line188"></a> *
<a name="line189"></a> * @param {string=} opt_type Type of event to remove, default is to
<a name="line190"></a> *     remove all types.
<a name="line191"></a> * @return {number} Number of listeners removed.
<a name="line192"></a> */
<a name="line193"></a>goog.events.Listenable.prototype.removeAllListeners;
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>/**
<a name="line197"></a> * Returns the parent of this event target to use for capture/bubble
<a name="line198"></a> * mechanism.
<a name="line199"></a> *
<a name="line200"></a> * NOTE(user): The name reflects the original implementation of
<a name="line201"></a> * custom event target ({@code goog.events.EventTarget}). We decided
<a name="line202"></a> * that changing the name is not worth it.
<a name="line203"></a> *
<a name="line204"></a> * @return {goog.events.Listenable} The parent EventTarget or null if
<a name="line205"></a> *     there is no parent.
<a name="line206"></a> */
<a name="line207"></a>goog.events.Listenable.prototype.getParentEventTarget;
<a name="line208"></a>
<a name="line209"></a>
<a name="line210"></a>/**
<a name="line211"></a> * Fires all registered listeners in this listenable for the given
<a name="line212"></a> * type and capture mode, passing them the given eventObject. This
<a name="line213"></a> * does not perform actual capture/bubble. Only implementors of the
<a name="line214"></a> * interface should be using this.
<a name="line215"></a> *
<a name="line216"></a> * @param {string|!goog.events.EventId.&lt;EVENTOBJ&gt;} type The type of the
<a name="line217"></a> *     listeners to fire.
<a name="line218"></a> * @param {boolean} capture The capture mode of the listeners to fire.
<a name="line219"></a> * @param {EVENTOBJ} eventObject The event object to fire.
<a name="line220"></a> * @return {boolean} Whether all listeners succeeded without
<a name="line221"></a> *     attempting to prevent default behavior. If any listener returns
<a name="line222"></a> *     false or called goog.events.Event#preventDefault, this returns
<a name="line223"></a> *     false.
<a name="line224"></a> * @template EVENTOBJ
<a name="line225"></a> */
<a name="line226"></a>goog.events.Listenable.prototype.fireListeners;
<a name="line227"></a>
<a name="line228"></a>
<a name="line229"></a>/**
<a name="line230"></a> * Gets all listeners in this listenable for the given type and
<a name="line231"></a> * capture mode.
<a name="line232"></a> *
<a name="line233"></a> * @param {string|!goog.events.EventId} type The type of the listeners to fire.
<a name="line234"></a> * @param {boolean} capture The capture mode of the listeners to fire.
<a name="line235"></a> * @return {!Array.&lt;goog.events.ListenableKey&gt;} An array of registered
<a name="line236"></a> *     listeners.
<a name="line237"></a> * @template EVENTOBJ
<a name="line238"></a> */
<a name="line239"></a>goog.events.Listenable.prototype.getListeners;
<a name="line240"></a>
<a name="line241"></a>
<a name="line242"></a>/**
<a name="line243"></a> * Gets the goog.events.ListenableKey for the event or null if no such
<a name="line244"></a> * listener is in use.
<a name="line245"></a> *
<a name="line246"></a> * @param {string|!goog.events.EventId.&lt;EVENTOBJ&gt;} type The name of the event
<a name="line247"></a> *     without the &#39;on&#39; prefix.
<a name="line248"></a> * @param {function(this:SCOPE, EVENTOBJ):(boolean|undefined)} listener The
<a name="line249"></a> *     listener function to get.
<a name="line250"></a> * @param {boolean} capture Whether the listener is a capturing listener.
<a name="line251"></a> * @param {SCOPE=} opt_listenerScope Object in whose scope to call the
<a name="line252"></a> *     listener.
<a name="line253"></a> * @return {goog.events.ListenableKey} the found listener or null if not found.
<a name="line254"></a> * @template SCOPE,EVENTOBJ
<a name="line255"></a> */
<a name="line256"></a>goog.events.Listenable.prototype.getListener;
<a name="line257"></a>
<a name="line258"></a>
<a name="line259"></a>/**
<a name="line260"></a> * Whether there is any active listeners matching the specified
<a name="line261"></a> * signature. If either the type or capture parameters are
<a name="line262"></a> * unspecified, the function will match on the remaining criteria.
<a name="line263"></a> *
<a name="line264"></a> * @param {string|!goog.events.EventId.&lt;EVENTOBJ&gt;=} opt_type Event type.
<a name="line265"></a> * @param {boolean=} opt_capture Whether to check for capture or bubble
<a name="line266"></a> *     listeners.
<a name="line267"></a> * @return {boolean} Whether there is any active listeners matching
<a name="line268"></a> *     the requested type and/or capture phase.
<a name="line269"></a> * @template EVENTOBJ
<a name="line270"></a> */
<a name="line271"></a>goog.events.Listenable.prototype.hasListener;
<a name="line272"></a>
<a name="line273"></a>
<a name="line274"></a>
<a name="line275"></a>/**
<a name="line276"></a> * An interface that describes a single registered listener.
<a name="line277"></a> * @interface
<a name="line278"></a> */
<a name="line279"></a>goog.events.ListenableKey = function() {};
<a name="line280"></a>
<a name="line281"></a>
<a name="line282"></a>/**
<a name="line283"></a> * Counter used to create a unique key
<a name="line284"></a> * @type {number}
<a name="line285"></a> * @private
<a name="line286"></a> */
<a name="line287"></a>goog.events.ListenableKey.counter_ = 0;
<a name="line288"></a>
<a name="line289"></a>
<a name="line290"></a>/**
<a name="line291"></a> * Reserves a key to be used for ListenableKey#key field.
<a name="line292"></a> * @return {number} A number to be used to fill ListenableKey#key
<a name="line293"></a> *     field.
<a name="line294"></a> */
<a name="line295"></a>goog.events.ListenableKey.reserveKey = function() {
<a name="line296"></a>  return ++goog.events.ListenableKey.counter_;
<a name="line297"></a>};
<a name="line298"></a>
<a name="line299"></a>
<a name="line300"></a>/**
<a name="line301"></a> * The source event target.
<a name="line302"></a> * @type {!(Object|goog.events.Listenable|goog.events.EventTarget)}
<a name="line303"></a> */
<a name="line304"></a>goog.events.ListenableKey.prototype.src;
<a name="line305"></a>
<a name="line306"></a>
<a name="line307"></a>/**
<a name="line308"></a> * The event type the listener is listening to.
<a name="line309"></a> * @type {string}
<a name="line310"></a> */
<a name="line311"></a>goog.events.ListenableKey.prototype.type;
<a name="line312"></a>
<a name="line313"></a>
<a name="line314"></a>/**
<a name="line315"></a> * The listener function.
<a name="line316"></a> * @type {function(?):?|{handleEvent:function(?):?}|null}
<a name="line317"></a> */
<a name="line318"></a>goog.events.ListenableKey.prototype.listener;
<a name="line319"></a>
<a name="line320"></a>
<a name="line321"></a>/**
<a name="line322"></a> * Whether the listener works on capture phase.
<a name="line323"></a> * @type {boolean}
<a name="line324"></a> */
<a name="line325"></a>goog.events.ListenableKey.prototype.capture;
<a name="line326"></a>
<a name="line327"></a>
<a name="line328"></a>/**
<a name="line329"></a> * The &#39;this&#39; object for the listener function&#39;s scope.
<a name="line330"></a> * @type {Object}
<a name="line331"></a> */
<a name="line332"></a>goog.events.ListenableKey.prototype.handler;
<a name="line333"></a>
<a name="line334"></a>
<a name="line335"></a>/**
<a name="line336"></a> * A globally unique number to identify the key.
<a name="line337"></a> * @type {number}
<a name="line338"></a> */
<a name="line339"></a>goog.events.ListenableKey.prototype.key;
</pre>


</body>
</html>
